package latisi.components {

import org.restfulx.Rx;
import org.restfulx.models.RxModel;
import org.restfulx.collections.ModelsCollection;
import latisi.models.Tblock;
import latisi.models.Section;
import flash.display.Sprite;

public class TblockSprite extends Sprite {

  public var tblock : Tblock;

  public function TblockSprite(tb:Tblock)
  {
    tblock = tb;
    super();
    this.tabEnabled = true;
  }

  public function drawTBSRectangle(options:Object = null):void {
    var lineWeight:int = 1;
    var fillAlpha:Number = 0.5;
    var fillColor:uint = getTbFillColor();
    var width:int = 0;
    var height:int = 0;
    if (options == null) options = {};
    if (options.hasOwnProperty("lineWeight")) lineWeight = options["lineWeight"];
    if (options.hasOwnProperty("fillAlpha")) fillAlpha = options["fillAlpha"];
    if (options.hasOwnProperty("fillColor")) fillColor = options["fillColor"];
    if (options.hasOwnProperty("width")) width = options["width"];
    if (options.hasOwnProperty("height")) height = options["height"];
    this.graphics.beginFill(fillColor, fillAlpha);
    this.graphics.lineStyle(lineWeight, getTbLineColor());
    if (tblock.nfRect == null) {
      tblock = (tblock.show() as Tblock);
    }
    this.graphics.drawRect(tblock.nfRect.x * width, tblock.nfRect.y * height, 
			   tblock.nfRect.width * width, tblock.nfRect.height * height);
    this.graphics.endFill();
    this.alpha = 0.5;
  }

  public function getTbFillColor():uint {
    var sections:ModelsCollection;
    var section:Section;
    if (tblock.classification) {
      sections = Rx.models.index(Section);
      section = (sections.withPropertyValue("name", tblock.classification) as Section);
      return section.color;
    } else if (tblock.mlClass) {
      sections = Rx.models.index(Section);
      section = (sections.withPropertyValue("name", tblock.mlClass) as Section);
      return section.color;
    } else {
      return 0xFFFFFF;
    }
  }

  public function getTbLineColor():uint {
    return 0x0;
  }


}

}
